package com.jack.leetcode.eachProblem06;

import java.util.Arrays;

public class 长度最小的子数组 {

    public static void main(String[] args) {
        int[] a = {2, 3, 1, 2, 4, 3};
        System.out.println(minSubArrayLen(7, a));
        int[] b = {12, 28, 83, 4, 25, 26, 25, 2, 25, 25, 25, 12};
        System.out.println(minSubArrayLen(213, b));
    }

    public static int minSubArrayLen(int s, int[] nums) {
        int n = nums.length;
        int result = Integer.MAX_VALUE;
        int i = 0;
        int j = 0;
        int sum = 0;
        while (i < n) {
            sum += nums[i];
            while (sum >= s) {
                result = Math.min(result, i - j + 1);
                sum -= nums[j];
                j++;
            }
            i++;
        }
        return result == Integer.MAX_VALUE ? 0 : result;
    }
}
